<?php

namespace ctrl;

use model\attments;
use model\siteconfig;
use common\Image;

class upload extends base
{
	const ROOTPATH = P_PUBLIC . 'uploads/';
	private static $siteConfig, $attments, $getConfig;
	private static $uploadPath = ['img' => 'image/', 'video' => 'video/', 'att' => 'attment/'];

	public static function Init()
	{
		parent::init();
		self::$attments = new attments;
		self::$siteConfig = new siteconfig;
		self::$getConfig = self::$siteConfig->getUpload();
	}

	//上传只走这一个控制器
	static function Upload()
	{
		$upValue = $_GET['up'];
		switch ($upValue) {
			case 'att':
				self::att();
				break;
			case 'video':
				self::video();
				break;
			default:
				self::img();
		}
	}

	private	static function Up($uploadPath, $ext, $size)
	{
		$up = new \libs\upload();
		$up->set('path', self::ROOTPATH . $uploadPath . date('Ym', time()) . '/' . date('d', time()));
		$up->set('allowType', $ext);
		$up->set('maxSize', $size * 1024);
		$up->upload();
		return [
			'info' => $up->getInfo(),
			'err' => $up->getError()
		];
	}


	private static function img()
	{
		if (isset($_GET['del']))
			die(DelFile($_POST['url']));
		$upResult = self::Up(self::$uploadPath['img'], explode('|', self::$getConfig['imguploadext']), self::$getConfig['imguploadsize']);

		if (empty($upResult['info'][0]['path'])) {
			if (isset($_GET['tinymce']))
				Response(array('res' => $upResult['err'], 'status' => 0));
			Response(array('status' => 0, 'info' => $upResult['err']));
		}

		if (!isset($_GET['nothumb'])) { //要求不缩略和无水印
			if (isset($_GET['profilePhoto'])) { //用户头像缩略
				Image::thumb($upResult['info'][0]['path'], $upResult['info'][0]['path'], '', 200, 200);
			} else {
				if (self::$getConfig['thumbON'] == 1)  Image::thumb($upResult['info'][0]['path'], $upResult['info'][0]['path'], '', self::$getConfig['maxWidth'], self::$getConfig['maxHeight']);
				if (self::$getConfig['waterON'] == 1)  Image::water($upResult['info'][0]['path'], P_PUBLIC . 'res/img/waterimg/logo.png', self::$getConfig['waterPos']);
			}
		}

		if (isset($_GET['tinymce']))
			Response(array('location' => $upResult['info'][0]['src'], 'status' => 200));
		Response(array('result' => $upResult['info'], 'status' => 1, 'info' => '上传成功'));
	}

	private	static function att()
	{
		if (isset($_GET['del']) && self::$attments->deleteOneData())
			Response(array('status' => 1, 'info' => '删除成功'));
		$upResult = self::Up(self::$uploadPath['att'], explode('|', self::$getConfig['attuploadext']), self::$getConfig['attuploadsize']);
		if ($upResult['info'][0]['path']) {
			//写入数据库，返回主键
			$prekey = self::$attments->insertData($upResult['info'][0]['src'], $upResult['info'][0]['originName'], $upResult['info'][0]['suffix'], $upResult['info'][0]['size']);
			SqlLog("上传附件 ID:{$prekey}");
			Response(array('result' => $upResult['info'], 'status' => 1, 'info' => '上传成功', 'prekey' => $prekey));
		} else {
			Response(array('status' => 0, 'info' => $upResult['err']));
		}
	}

	private	static function video()
	{
		if (isset($_GET['del']))
			die(DelFile($_POST['url']));
		$upResult = self::Up(self::$uploadPath['video'], explode('|', self::$getConfig['videouploadext']), self::$getConfig['videouploadsize']);
		if ($upResult['info'][0]['path']) {
			SqlLog("上传视频：" . $upResult['info'][0]['src']);
			Response(array('result' => $upResult['info'], 'status' => 1, 'info' => '上传成功'));
		} else {
			Response(array('status' => 0, 'info' => $upResult['err']));
		}
	}
}
